const Net = require('../../common.js')
Page({
  data: {
    viewHight: 0,
    viewTitleHight: 0,
    info: [],
    NowPage: 1,
    pageBackGround: 'rgba(32, 73, 146, 0)',
    pageColor: '#fff',
    pullDownHeight: 0,
    onPullDwonStatus: false,
    nowScrpllNumber: 0,
    onsss: ''
  },
  onLoad: function (options) {
    var _this = this
    var token = wx.getStorageSync('access_token')
    if (!token) {
      Net.PageRoter('_loginGuide', '', 'rel')
    }
    var sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    Net.data({
      status: statusHeight,
      navHeight: navHeight
    })
    setTimeout(() => {
      const query = wx.createSelectorQuery()
      query.select('#pageimg').boundingClientRect()
      query.select('#pagetitle').boundingClientRect()
      query.select('#listIcon').boundingClientRect()
      query.exec(function (res) {
        Net.data({
          viewHight: res[0].height,
          // bannerView: res[1].height,
          fixedHeight: res[2].height
        })
      })
    }, 1000)
    _this.onPageInfo('6')
  },
  onPullStare(e) {
    Net.data({
      nowScrpllNumber: e.touches[0].clientY,
      onsss: 'up'
    })
  },
  onPullDownMove(e) {
    console.log('运行了移动方法')
    if (e.touches[0].clientY > Net.data('nowScrpllNumber')) {
      Net.data({
        onPullDwonStatus: true
      })
      if (Net.data('onPullDwonStatus')) {
        Net.data({
          pullDownHeight: 70,
          NowPage: 1
        })
      }
    }
  },
  onPullEnd(e) {
    console.log(e)
    if (Net.data('onPullDwonStatus')) {
      Net.data({
        onsss: ''
      })
      this.onPageInfo()
    }
  },
  onPageInfo(e) {
      Net.link('v1/ruleList', {
        now_page: Net.data('NowPage')
      }, function (res) {
        if (!e) {
          Net.data({
            pullDownHeight: null,
            onPullDwonStatus: false
          })
          setTimeout(() => {
            Net.data({
              pullDownHeight: 0
            })
          }, 1500);
        }
        console.log('第一个返回的数据', res)
        if (res.data.code == 6 || res.data.mag == "Token验证失败") {
          Net.filaModal()
        }
        if (res.data.data.list.length == 0 && Net.data('NowPage') != 1) {
          Net.toast('已加载全部数据', 'none')
          return false
        }
        for (let i in res.data.data.list) {
          res.data.data.list[i].create_time = Net.formatTime(res.data.data.list[i].create_time, 'Y-M-D h:m:s')
        }
        if (Net.data('NowPage') != 1) {
          var list = Net.data('info')
          for (let i in res.data.data.list) {
            list.push(res.data.data.list[i])
          }
          Net.data({
            info: list
          })
        } else {
          Net.data({
            info: res.data.data.list
          })
        }
        
        Net.hid()
      }, 'GET', function (err) {})
      if (Net.data('NowPage') == 1) {
        Net.link('v1/store', {}, function (rep) {
          Net.data({
            priceInfo: rep.data.data
          });
          try {
            wx.setStorageSync('store', rep.data.data.member_mall.id)
          } catch (error) {}
          if (rep.data.code != 200 && rep.data.code != 201) {
            wx.removeStorageSync('access_token')
            wx.removeStorageSync('store')
            // Net.PageRoter('_loginGuide', '', 'rel')
          }
        }, "GET")
      }

  },
  onShopAdministration(e) {
    var id = e.currentTarget.id
    if (id == 1) {
      Net.PageRoter('_userShopAdministration', '', 'nav')
    } else if (id == 2) {
      Net.PageRoter('_goodsSql', '', 'nav')
    } else {
      Net.toast('努力开发中 敬请期待', 'none')
    }
  },
  onCreateActivity() {
    Net.PageRoter('_Activity', '', 'swi')
  },
  // errImg: function (e) {
  //   var _errImg = e.target.dataset.errImg;
  //   var _objImg = "'" + _errImg + "'";
  //   var _errObj = {};
  //   _errObj[_errImg] = "http://img3.imgtn.bdimg.com/it/u=3361194144,3728200903&fm=26&gp=0.jpg";
  //   console.log(e.detail.errMsg + "----" + _errObj[_errImg] + "----" + _objImg);
  //   Net.data(_errObj)
  // },
  onUserOrder(e) {
    Net.PageRoter('_onUserOrderInfo', '?type=' + e.currentTarget.id, 'nav')
  },
  onGoOrder() {
    Net.PageRoter('_OrderList', '', 'swi')
  },
  onPageScroll(e) {
    var _this = this
    if (e.scrollTop > 0) {
      Net.data({
        onPullDwonStatus: false
      })
    }
    if (e.scrollTop == 0) {
      Net.data({
        onPullDwonStatus: true
      })
    }
    if (Number(e.scrollTop) > 50) {
      if (Net.data('pageBackGround') == 'rgba(32, 73, 146, 1)') {
        return false
      }
      console.log('大于')
      Net.data({
        pageBackGround: 'rgba(32, 73, 146, 1)',
        pageColor: '#fff'
      })
    } else {
      if (Net.data('pageBackGround') == 'rgba(32, 73, 146, 0)') {
        return false
      }
      console.log('!大于')
      Net.data({
        pageBackGround: 'rgba(32, 73, 146, 0)',
        pageColor: '#fff'
      })
    }
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {
    Net.toast('加载中', 'loading')
    Net.data({
      NowPage: Net.data('NowPage') + 1
    })
    this.onPageInfo('6')
  },
  onShareAppMessage: function () {}
})